/*
 * VistaPaquetes.java
 *
 * Created on 20 de noviembre de 2008, 04:43 PM
 * Author : Julio Nevado
 */

package ddta;

import java.util.ArrayList;
import javax.swing.DefaultComboBoxModel;
import javax.swing.DefaultListSelectionModel;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author  julio
 */
public class VistaPaquetes extends JDialog {

    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,id=DCE.6F899B74-C652-FBCF-EE05-B17554569865]
    // </editor-fold> 
    private DDTA ddta;
    private DefaultTableModel tableModel;
    /** Creates new form VistaPaquetes */
    public VistaPaquetes(JDialog parent, boolean modal, DDTA ddta, String codAgencia) {
        super(parent, modal);
        initComponents();
        this.ddta = ddta;
        tblTours.setSelectionMode(DefaultListSelectionModel.SINGLE_SELECTION);
        this.tableModel = (DefaultTableModel) tblTours.getModel();
        mostrarAgencia(codAgencia);
        mostrarPaquetes(codAgencia);
        rellenarComboLugares();
        rellenarComboTarifas();
        rellenarComboTipoLugares();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        txtNombreTour = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        txtCodigoTour = new javax.swing.JTextField();
        btnGuardar = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        cboCodTarifa = new javax.swing.JComboBox();
        btnTarifas = new javax.swing.JButton();
        cboLugares = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        lstLugares = new javax.swing.JList();
        btnCancelarEdit = new javax.swing.JButton();
        cboTipLugares = new javax.swing.JComboBox();
        txtCodAgencia = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        txtAgenciaNombre = new javax.swing.JTextField();
        jPanel3 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblTours = new javax.swing.JTable();
        btnEditarTour = new javax.swing.JButton();
        btnSalir = new javax.swing.JButton();
        popupTarifa = new javax.swing.JInternalFrame();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Tours turisticos");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Paquetes tours - Agencia"));

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Tours turisticos"));

        jLabel3.setText("Nombre de tour :");

        jLabel4.setText("Código de tour :");

        btnGuardar.setText("Guardar tour");
        btnGuardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnGuardarActionPerformed(evt);
            }
        });

        jLabel5.setText("Lugar turístico :");

        jLabel6.setText("Cod. Tarifa :");

        btnTarifas.setText("Nuevas Tarifas...");
        btnTarifas.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnTarifasActionPerformed(evt);
            }
        });

        cboLugares.setText("Lugares turísticos...");
        cboLugares.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cboLugaresActionPerformed(evt);
            }
        });

        lstLugares.setVisibleRowCount(3);
        jScrollPane2.setViewportView(lstLugares);

        btnCancelarEdit.setText("Cancelar edición");
        btnCancelarEdit.setEnabled(false);
        btnCancelarEdit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarEditActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel6)
                    .addComponent(jLabel5)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel4)
                            .addComponent(jLabel3))
                        .addGap(34, 34, 34)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addComponent(btnGuardar)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(btnCancelarEdit)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(cboTipLugares, javax.swing.GroupLayout.PREFERRED_SIZE, 154, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(cboLugares)
                                    .addComponent(btnTarifas))
                                .addGap(22, 22, 22))
                            .addComponent(txtCodigoTour, javax.swing.GroupLayout.PREFERRED_SIZE, 111, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(cboCodTarifa, 0, 586, Short.MAX_VALUE)
                            .addComponent(txtNombreTour, javax.swing.GroupLayout.DEFAULT_SIZE, 586, Short.MAX_VALUE)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 586, Short.MAX_VALUE))))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(txtCodigoTour, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(txtNombreTour, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel5)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(6, 6, 6)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(5, 5, 5)
                        .addComponent(jLabel6))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cboCodTarifa, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 17, Short.MAX_VALUE)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(btnGuardar)
                            .addComponent(btnCancelarEdit))
                        .addGap(34, 34, 34))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(6, 6, 6)
                        .addComponent(btnTarifas)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(cboLugares)
                            .addComponent(cboTipLugares, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addContainerGap())))
        );

        txtCodAgencia.setEditable(false);

        jLabel2.setText("Agencia :");

        txtAgenciaNombre.setEditable(false);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addGap(33, 33, 33)
                        .addComponent(txtCodAgencia, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(txtAgenciaNombre, javax.swing.GroupLayout.DEFAULT_SIZE, 561, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtCodAgencia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtAgenciaNombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(34, 34, 34))
        );

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createTitledBorder(""), "Lista de Tours turisticos"));

        tblTours.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Código", "Nombre del tour", "Cod. Lugar", "Cod. Tarifa", "# ventas"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(tblTours);
        tblTours.getColumnModel().getColumn(0).setPreferredWidth(50);
        tblTours.getColumnModel().getColumn(1).setPreferredWidth(200);

        btnEditarTour.setText("Editar tour");
        btnEditarTour.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEditarTourActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 743, Short.MAX_VALUE)
                    .addComponent(btnEditarTour))
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 148, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(btnEditarTour))
        );

        btnSalir.setText("  Cerrar  ");
        btnSalir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSalirActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout popupTarifaLayout = new javax.swing.GroupLayout(popupTarifa.getContentPane());
        popupTarifa.getContentPane().setLayout(popupTarifaLayout);
        popupTarifaLayout.setHorizontalGroup(
            popupTarifaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );
        popupTarifaLayout.setVerticalGroup(
            popupTarifaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGap(12, 12, 12)
                        .addComponent(popupTarifa, javax.swing.GroupLayout.PREFERRED_SIZE, 0, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(btnSalir)
                            .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGap(24, 24, 24))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(247, 247, 247)
                        .addComponent(popupTarifa, javax.swing.GroupLayout.PREFERRED_SIZE, 0, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 349, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(18, 18, 18)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnSalir)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
private void limpiarDatos(){
   txtNombreTour.setText("");
   lstLugares.setSelectedIndices(new int[]{-1});
   cboCodTarifa.setSelectedIndex(-1);
   txtCodigoTour.setText("");
}
private void btnGuardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnGuardarActionPerformed
// TODO add your handling code here:
    if (evt.getActionCommand().equals("EDITAR")) {
       actualizarPaquete();
    } else {
       registrarPaquete();
    }
    btnCancelarEdit.setEnabled(false);
}//GEN-LAST:event_btnGuardarActionPerformed

private void btnSalirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSalirActionPerformed
// TODO add your handling code here:
    this.setVisible(false);
    enviarMensages();
    this.dispose();
}//GEN-LAST:event_btnSalirActionPerformed

private void btnTarifasActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnTarifasActionPerformed
// TODO add your handling code here:
    VistaTarifas frmTarifas = new VistaTarifas(this, true, this.ddta);
    frmTarifas.setVisible(true);
    frmTarifas.dispose();
}//GEN-LAST:event_btnTarifasActionPerformed

private void cboLugaresActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cboLugaresActionPerformed
// TODO add your handling code here:
    String codTipoLugar = ((TipoLugar)cboTipLugares.getSelectedItem()).getCodigo();
    VistaLugaresTuristicos frmLugares = new VistaLugaresTuristicos(this, true, this.ddta, codTipoLugar);
    frmLugares.setVisible(true);
}//GEN-LAST:event_cboLugaresActionPerformed

private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
// TODO add your handling code here:
    enviarMensages();
}//GEN-LAST:event_formWindowClosing

private void btnEditarTourActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEditarTourActionPerformed
// TODO add your handling code here:
    int iSel = tblTours.getSelectedRow();
    if (iSel >= 0) {
        String codTour = this.tableModel.getValueAt(iSel, 0).toString();
        btnGuardar.setActionCommand("EDITAR");
        mostrarDatosTour(codTour);
    } else {
        JOptionPane.showMessageDialog(this, "Seleccione un paquete");
    }
}//GEN-LAST:event_btnEditarTourActionPerformed

private void btnCancelarEditActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarEditActionPerformed
// TODO add your handling code here:
    btnCancelarEdit.setEnabled(false);
    limpiarDatos();
    btnGuardar.setActionCommand("GUARDAR");
}//GEN-LAST:event_btnCancelarEditActionPerformed

    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,regenBody=yes,id=DCE.38A4D75B-8F9E-6781-06C3-DD927C188461]
    // </editor-fold> 
    public DDTA getDdta () {
        return ddta;
    }

    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,regenBody=yes,id=DCE.46416610-62E7-9C02-BEA9-3B9E9A5927C0]
    // </editor-fold> 
    public void setDdta (DDTA val) {
        this.ddta = val;
    }
    private void enviarMensages() {
        if (this.getParent() != null) {
            if (this.getParent() instanceof VistaReservasPaquetes) {
                ((VistaReservasPaquetes)this.getParent()).mostrarPaquetes(txtCodAgencia.getText());
            }
        }
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancelarEdit;
    private javax.swing.JButton btnEditarTour;
    private javax.swing.JButton btnGuardar;
    private javax.swing.JButton btnSalir;
    private javax.swing.JButton btnTarifas;
    private javax.swing.JComboBox cboCodTarifa;
    private javax.swing.JButton cboLugares;
    private javax.swing.JComboBox cboTipLugares;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JList lstLugares;
    private javax.swing.JInternalFrame popupTarifa;
    private javax.swing.JTable tblTours;
    private javax.swing.JTextField txtAgenciaNombre;
    private javax.swing.JTextField txtCodAgencia;
    private javax.swing.JTextField txtCodigoTour;
    private javax.swing.JTextField txtNombreTour;
    // End of variables declaration//GEN-END:variables
    
    private void registrarPaquete() {
       String codigoPaquete = txtCodigoTour.getText();
       String codigoTarifa;
       try {
          codigoTarifa = ((Tarifa)cboCodTarifa.getSelectedItem()).getCodigoTarifa();
       } catch(Exception e) {
           codigoTarifa = "";
       }
       String codigoAgencia = txtCodAgencia.getText();
       int lugaresSelec = lstLugares.getSelectedValues().length;
       String nombrePaquete = txtNombreTour.getText();
       if (codigoPaquete.equals("") || codigoTarifa.equals("") || nombrePaquete.equals("") || lugaresSelec == 0) {
            JOptionPane.showMessageDialog(this, "Debe ingresar todos los datos", this.getTitle(), JOptionPane.ERROR_MESSAGE);
       } else {
           PaqueteTour tour = this.ddta.getCtlPaquetes().buscarPaquete(codigoPaquete);
           if (tour == null) {
               Agencia agencia = this.ddta.getCtlAgencias().buscarAgencia(codigoAgencia);
               Tarifa tarifa = this.ddta.getCtlTarifas().buscarTarifa(codigoTarifa);
               if (tarifa == null) {
                   JOptionPane.showMessageDialog(this, "Codigo de Tarifa no existe", this.getTitle(), JOptionPane.ERROR_MESSAGE);
                   return;
               }
               ArrayList<LugarTuristico> lugares = new ArrayList<LugarTuristico>();
               for (Object item : lstLugares.getSelectedValues()) {
                   lugares.add((LugarTuristico)item);
               }
               this.ddta.getCtlPaquetes().registrarPaqueteTour(codigoPaquete, nombrePaquete, agencia, lugares, tarifa);
               mostrarPaquetes(codigoAgencia);
               limpiarDatos();
               btnGuardar.setActionCommand("GUARDAR");
               txtCodigoTour.requestFocusInWindow();
            } else {
               JOptionPane.showMessageDialog(this, "Código ya esta registrado como :" + tour.getNombrePaquete());
            }
       }
    }
  
     private void actualizarPaquete() {
       String codigoPaquete = txtCodigoTour.getText();
       String codigoTarifa = ((Tarifa)cboCodTarifa.getSelectedItem()).getCodigoTarifa();
       String codigoAgencia = txtCodAgencia.getText();
       int lugaresSelec = lstLugares.getSelectedValues().length;
       String nombrePaquete = txtNombreTour.getText();
       if (codigoPaquete.equals("") || codigoTarifa.equals("") || nombrePaquete.equals("") || lugaresSelec == 0) {
            JOptionPane.showMessageDialog(this, "Debe ingresar todos los datos", this.getTitle(), JOptionPane.ERROR_MESSAGE);
       } else {
           Agencia agencia = this.ddta.getCtlAgencias().buscarAgencia(codigoAgencia);
           Tarifa tarifa = this.ddta.getCtlTarifas().buscarTarifa(codigoTarifa);
           if (tarifa == null) {
               JOptionPane.showMessageDialog(this, "Codigo de Tarifa no existe", this.getTitle(), JOptionPane.ERROR_MESSAGE);
               return;
           }
           ArrayList<LugarTuristico> lugares = new ArrayList<LugarTuristico>();
           for (Object item : lstLugares.getSelectedValues()) {
               lugares.add((LugarTuristico)item);
           }
           this.ddta.getCtlPaquetes().actualizarPaquete(codigoPaquete, nombrePaquete, agencia, lugares, tarifa);
           mostrarPaquetes(codigoAgencia);
           limpiarDatos();
           btnGuardar.setActionCommand("GUARDAR");
           txtCodigoTour.requestFocusInWindow();
       }
    }
    
    private void mostrarPaquetes(String codAgencia) {
        Agencia agencia = this.ddta.getCtlAgencias().buscarAgencia(codAgencia);
        if (agencia != null) {
            ArrayList<PaqueteTour> paquetesAgencia = this.ddta.getCtlPaquetes().filtrarPaquetes(agencia);
            if (paquetesAgencia.size() > 0) {
                this.tableModel.setNumRows(0);
                for (PaqueteTour p : paquetesAgencia) {
                    this.tableModel.addRow(new Object[]{
                        p.getCodigoTour(),
                        p.getNombrePaquete(),
                        p.getRutaTurisiticas(),
                        p.getTarifa().getCodigoTarifa(),
                        this.ddta.obtenerNumPaquetesVendxTour(p)
                    });
                }
            }
            
        }
    }
    private void mostrarAgencia(String codAgencia) {
        Agencia agencia = this.ddta.getCtlAgencias().buscarAgencia(codAgencia);
        if (agencia != null) {
            txtCodAgencia.setText(agencia.getCodigoAgencia());
            txtAgenciaNombre.setText(agencia.getNombreAgencia());
        }
    }

    public void rellenarComboTarifas() {
        ((DefaultComboBoxModel)cboCodTarifa.getModel()).removeAllElements();
        if (this.ddta.getCtlTarifas().getTarifas().size() > 0) {
            for (Tarifa tar : this.ddta.getCtlTarifas().getTarifas()) {
                 cboCodTarifa.addItem(tar);
            }
        }
    }
    public void rellenarComboLugares(String codigoTipoLugar) {
        TipoLugar tipoLugar = this.ddta.getCtlTipoLugar().buscarTipoLugar(codigoTipoLugar);
        lstLugares.removeAll();
        ArrayList<LugarTuristico> lugares = this.ddta.getCtlLugaresTuristicos().filtrarLugaresTuristicos(tipoLugar);
        if (lugares.size() > 0) {
            lstLugares.setListData(lugares.toArray());
        }
    }
    public void rellenarComboLugares() {
        lstLugares.removeAll();
        if (this.ddta.getCtlLugaresTuristicos().getLugaresTuristicos().size() > 0) {
            lstLugares.setListData(this.ddta.getCtlLugaresTuristicos().getLugaresTuristicos().toArray());
        }
    }
    public void rellenarComboTipoLugares() {
        cboTipLugares.removeAll();
        if (this.ddta.getCtlTipoLugar().getTiposLugarers().size() > 0) {
            for (TipoLugar tipo : this.ddta.getCtlTipoLugar().getTiposLugarers()) {
                cboTipLugares.addItem(tipo);
            }
        }
    }
    public void mostrarDatosTour(String codTour) {
        if (JOptionPane.showConfirmDialog(this, "Desea editar este Tour", "Editar tour?", JOptionPane.OK_CANCEL_OPTION) == JOptionPane.OK_OPTION) {
            PaqueteTour paquete = this.ddta.getCtlPaquetes().buscarPaquete(codTour);
            txtCodigoTour.setText(paquete.getCodigoTour());
            txtNombreTour.setText(paquete.getNombrePaquete());
            cboCodTarifa.setSelectedItem(paquete.getTarifa());
            int[] indexes = new int[paquete.getLugaresTuristicos().size()];
            int index = 0;
            for (int i = 0; i < lstLugares.getModel().getSize(); i++) {
                LugarTuristico itemLugar = (LugarTuristico)lstLugares.getModel().getElementAt(i);
                for (int j = 0; j < indexes.length; j++) {
                    LugarTuristico lugar =  paquete.getLugaresTuristicos().get(j);
                    if (lugar.getCodigoLugar().equals(itemLugar.getCodigoLugar())) {
                        if (index < indexes.length) {
                            indexes[index] = i;
                            index++;
                        }
                    }
                }
            }
            lstLugares.setSelectedIndices(indexes);
            btnCancelarEdit.setEnabled(true);
        }
        
    }
}
